﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


namespace WebApplication1.Daywtwo
{
    public partial class Longer : System.Web.UI.Page
    {
        Sqlhyper Sqlhyper = new Sqlhyper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridViewText();
            }
        }
        private void GridViewText() {
            //GridView1.DataKeyNames = new string[] { "stu_id" };
            GridView1.DataKeyNames = new string[] { "stu_id" };
            GridView1.DataSource = Sqlhyper.Gettable("select*from StudentInfo", null);
            GridView1.DataBind();
        }

        //查找
        protected void Button1_Click(object sender, EventArgs e)
        {
                 
            
            //'%' + @name + '%'
            string sql = "select*from StudentInfo where stu_name like '%'+@TextBox1+'%'";

            SqlParameter[] parameters ={
               new SqlParameter("@TextBox1",TextBox1.Text)
            };


            GridView1.DataSource =Sqlhyper.Gettable(sql, parameters);
            GridView1.DataBind();

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string sql = "insert into StudentInfo (stu_name,email,intro) values(@2,@3,@4)";
            SqlParameter[] parameters = {
              new SqlParameter("@2",TextBox3.Text),
              new SqlParameter("@3",TextBox4.Text),
              new SqlParameter("@4",TextBox5.Text)
            };
            if (Sqlhyper.NotFind(sql, parameters))
            {
                Literal1.Text = "添加成功！！！";
                GridViewText();
            }
            else {
                Literal1.Text = "添加失败！！！";
            }
        }

        protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            GridViewText();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
           
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();

            string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            string email = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            string intro = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
            string paw= ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text;

            string sql = "update StudentInfo set stu_name=@1,email=@2,intro=@3,password=@4 where stu_id=@id";

            SqlParameter[] parameters = {
              new SqlParameter("@1",name),
              new SqlParameter("@2",email),
              new SqlParameter("@3",intro),
              new SqlParameter("@4",paw),
               new SqlParameter("@id",id)
            };
            if (Sqlhyper.NotFind(sql, parameters))
            {
                Literal1.Text = "更新成功！！！";
                GridViewText();
            }
            else {
                Literal1.Text = "更新失败！！！";
            }

        }

       
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            GridViewText();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string sql = "delete from StudentInfo where stu_id=@id";
            SqlParameter[] parameters = {
            new SqlParameter("@id",id)
            };
            if (Sqlhyper.NotFind(sql, parameters))
            {
                Literal1.Text = "删除成功！！！";
                GridViewText();
            }
            else
            {
                Literal1.Text = "删除失败！！！";
            }
        }
    }
}